$(function(){
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组
    var root_url = $("#root_url").val();

    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent(){
        //根据条件查询订单信息
        $('.inner-section #search_btn').click(function() {
            render(true,1,pageSize);
            return false;
        });

        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function(event){
            event = event ? event: window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
                return false;
            }
        });
    }

    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function(i, field){
            selectInfo[field.name]=field.value;
        });
        JSON.stringify(selectInfo);
        return selectInfo;
    }
    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);

    }
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingWithdrawal',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var myList = result.data.dataList;

                    html+='<tr>' +
                        '<th class="th1">序号</th>' +
                        '<th class="">会员</th>' +
                        '<th class="" style="width: 240px;">提现账号</th>' +
                        '<th class="">提现方式</th>' +
                        '<th class="">申请提现金额</th>' +
                        '<th class="">手续费</th>' +
                        '<th class="">实际打款金额</th>' +
                        '<th class="">申请提现时间</th>' +
                        '<th class="">审核状态</th>' +
                        '<th class="">审核时间</th>' +
                        '<th class="">操作</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < myList.length;i++){
                        var obj = myList[i];
                        var num = (pageIndex-1)*pageSize + i+1;
                        var amount = obj.amount; //提现金额
                        var payment = obj.payment; //提现方式
                        var add_time = obj.add_time; //提现时间
                        var audit_status = obj.audit_status; //审核状态
                        var audit_time = obj.audit_time; //审核时间
                        var audit_msg = obj.audit_msg; //审核说明
                        var nick_name = obj.nick_name; //用户
                        var username = obj.username;
                        var memberUsername = obj.memberUsername;
                        var account = obj.account;
                        var bankname = obj.bankname||'--';
                        var id = obj.id;
                        var checked = (idList.indexOf(id) >= 0) ? "checked":"";//判断当前记录先前有没有被选中
                        var payment_text = '微信';
                        var audit_status_text = '待审核';
                        var btn_text = '--';
                        var type_text = '微信';

                        if (payment == 1)payment_text = '支付宝';
                        if (payment == 2)payment_text = '银行卡';

                        var audit_state_text = '<span style="color: grey;">待审核</span>';
                        if (audit_status == 1) audit_state_text = '<span style="color: green;">通过</span>';
                        if (audit_status == 2) audit_state_text = '<span style="color: red;">驳回</span>';
                        if (audit_status == 0) {
                            btn_text = '<a href="javascript:;" style="margin-bottom: 2px" class="btn btn-success btn-xs auditStatus" data-status="1" data-id = "'+id+'">审核</a>';
                        }

                        var withdrawal_account_html = '';
                        //withdrawal_account_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">类型：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + payment_text + '</span></div>';
                        withdrawal_account_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">账号：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + account + '</span></div>';
                        withdrawal_account_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">银行：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + bankname + '</span></div>';
                        withdrawal_account_html += '<div style="font-size: 12px;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"><span style="color: #979b9e;">用户名：</span><span style="color: #0b0f12;margin-left: 10px;overflow: hidden;">' + username + '</span></div>';

                        if (audit_time == null) {
                            audit_time = ''
                        }
                        html += '<tr>'
                            + '<td class="th1">' + num + '</td>'
                            + '<td class="th2">' + (memberUsername == null ? nick_name : memberUsername) + '</td>'
                            + '<td class="" style="text-align: unset;">' +
                            '<div style="" class="info">' +
                            '<div class="left" style="padding: 0 10px;display: flex;flex-direction: column;justify-content: space-between;align-items: center;width: 100%;">' +
                            withdrawal_account_html +
                            '</div>' +
                            '</div>' +
                            '</td>'
                            + '<td class="th2">' + payment_text + '</td>'
                            + '<td class="th2">' + amount + '</td>'
                            + '<td class="th2">' + obj.service_fee + '</td>'
                            + '<td class="th2">' + obj.trans_money + '</td>'
                            + '<td class="th2">' + add_time + '</td>'
                            + '<td class="th2">' + audit_state_text + '</td>'
                            + '<td class="th2">' + audit_time + '</td>'
                            + '<td>'
                            + btn_text
                            //+'<a href="detailWithdrawal?id='+id+'" data-id="'+id+'" class="btn btn-primary btn-xs edit-order">详情</a>'
                            //+'<a href="javascript:;" data-id="'+id+'" class="btn btn-default btn-xs delete-order">删除</a>'
                            + '</td>'
                            + '</tr>';
                    }

                    if(myList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                        $("#list-table tbody").html(html);
                    }else{
                        $("#list-table tbody").html(html);
                        myCheck();
                        batchSelect(idList,".inner-section #list-table .select-all",".inner-section #list-table .select-single");
                       
                        //删除
                        $(".delete-order").click(deleteActivityClassify);
                        //审核
                        $("#list-table .auditStatus ").click(auditStatus);
                    }

                }else{
                    responseTip(result.errorCode,result.errorInfo,1500);
                }
            },
            error:errorResponse
        });
    }

    /**
     * 审核
     */
    function auditStatus(){
        var that = $(this)
        layer.confirm('请选择审核状态?',{
            title:'提示',
            btn:['通过审核','审核失败'],
            btn1:function (index,layero) {
                agree(that,index,layero)
            },
            btn2:function (index) {
                reject(that,index)
            },
        })
    }

    // 审核同意
    function agree(that,index,layero) {
        var id = that.attr("data-id");
        $.ajax({
            type:"post",
            url:'auditStatus',
            data:{
                "id":id,'state':1
            },
            dataType:'json',
            success:function(json,statusText){
                if(json.errorCode == 0){
                    if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                        currentPage = currentPage - 1;
                    }
                    render(true,currentPage,pageSize);
                }else{
                    responseTip(json.errorCode,json.errorInfo,1500);
                }
            }
        })
        layer.closeAll();
    }

    // 审核拒绝
    function reject (that,index) {
        var id = that.attr("data-id");
        layer.closeAll();
        layer.prompt({title: '原因', formType: 2}, function(reason, index){
            if(reason == ''){
                return false;
            }
            $.ajax({
                type:"post",
                url:'auditStatus',
                data:{
                    "id":id,'state':2,'reason':reason
                },
                dataType:'json',
                success:function(json,statusText){
                    layer.closeAll();
                    if(json.errorCode == 0){
                        if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                            currentPage = currentPage - 1;
                        }
                        render(true,currentPage,pageSize);
                    }else{
                        responseTip(json.errorCode,json.errorInfo,1500);
                    }
                }
            })
        });
    }

    /**
     * 删除
     * @param aid
     * @param state
     */
    function deleteActivityClassify(){
        var id = $(this).attr('data-id');
        myConfirmModal("确定删除吗？",function(){
            $.ajax(
                {
                    type:"post",
                    url:"deleteWithdrawal",
                    data:{"id":id},
                    dataType:"json",
                    beforeSend:function(xhr){
                        //显示“加载中。。。”
                        $("#loading").modal('show');
                    },
                    complete:function(){
                        //隐藏“加载中。。。”
                        $("#loading").modal('hide');
                    },
                    success:function(json,statusText){
                        if(json.errorCode == 0){
                            if(currentPage !=1 && total_count % pageSize == 1){//非首页且末页记录数为1时
                                currentPage = currentPage - 1;
                            }
                            render(true,currentPage,pageSize);
                        }else{
                            responseTip(json.errorCode,json.errorInfo,1500);
                        }
                    },
                    error:errorResponse
                }
            );
        });
    }
    
    init();
});